package cn.edu.ctbu.simplescoremanagementsystem.webapi;

import cn.edu.ctbu.simplescoremanagementsystem.domain.Student;
import cn.edu.ctbu.simplescoremanagementsystem.service.StudentService;
import cn.edu.ctbu.simplescoremanagementsystem.utils.ResultUtils;
import cn.edu.ctbu.simplescoremanagementsystem.vo.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
@RequestMapping("/webapi/login")
public class LoginRestController {
    @Autowired
    StudentService studentService;

    @GetMapping("/validata")
    public Result<Integer> user(HttpServletRequest httpRequest, String username, String password){
        List<Student> students = studentService.findByNoAndPassword(username,password);
        if(students.size()>0){
            HttpSession session = httpRequest.getSession();
            session.setAttribute("student",students.get(0).getId());
            session.setAttribute("admin",null);
            return ResultUtils.success(students.get(0).getId());
        }else {
            if (username.equals("admin")&&password.equals("123")){
                HttpSession session = httpRequest.getSession();
                session.setAttribute("student",null);
                session.setAttribute("admin",2);
                return ResultUtils.success(0);
            }
            return ResultUtils.error(-1);
        }
    }

    @GetMapping("/logout")
    public Result<Integer> logout(HttpServletRequest httpRequest){
        HttpSession session = httpRequest.getSession();
        if (session != null) {
            session.invalidate(); // 清空当前用户的session
            return ResultUtils.success(1);
        } else {
            return ResultUtils.error(-1);
        }
    }

    @GetMapping("/who")
    public Result<Integer> who(HttpServletRequest httpRequest){
        HttpSession session = httpRequest.getSession();
        if (session != null) {
            if(session.getAttribute("student") ==null) {
                if (session.getAttribute("admin") == null) {
                    return ResultUtils.error(-1);
                } else {
                    return ResultUtils.success(2);
                }
            }else{
                Student student = studentService.findById((Integer) session.getAttribute("student")).getData();
                return ResultUtils.success(student.getSname());
            }
        } else {
            return ResultUtils.error(-1);
        }
    }

    @PutMapping("/changePassword")
    public Result<Integer> changePassword(HttpServletRequest httpRequest,String oldPassword,String newPassword){
        HttpSession session = httpRequest.getSession();
        if (session != null) {
            int id = (Integer) session.getAttribute("student");
            if(id > 0){
                Student student = studentService.findById(id).getData();
                if(!student.getPassword().equals(oldPassword)){
                    return ResultUtils.error(-1);
                }
                student.setPassword(newPassword);
                studentService.update(student);
            }else if(id == 0){ //管理员
                return ResultUtils.error(-1);
            }
            return ResultUtils.success(1);

        } else {
            return ResultUtils.error(-1);
        }
    }

}